package com.example.flshtarvel.controller;

import com.example.flshtarvel.entity.PageResult;
import com.example.flshtarvel.entity.Tag;
import com.example.flshtarvel.service.TagArticleMapService;
import com.example.flshtarvel.service.TagService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.sql.ResultSet;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class TagController {
    @Autowired
    TagService tagService;
    @Autowired
    TagArticleMapService tagArticleMapService;

    @GetMapping("/tag/{tagId}")
    public Map<String,Object> selectTagById(@PathVariable("tagId") String tagId){
        Map<String,Object> map =  new HashMap<String,Object>();
        Tag tagResult = tagService.selectTagById(tagId);
        if(tagResult==null){
            map.put("msg",0);
        }else{
            map.put("msg",1);
            map.put("tag",tagResult);
        }
        return map;
    }

    @GetMapping("/tag/selectTagByName/{tagName}")
    public Map<String,Object> selectTagByName(@PathVariable("tagName") String tagName){
        Map<String,Object> map =  new HashMap<String,Object>();
        List<Tag> tagListResult = tagService.selectTagByName(tagName);
        if(tagListResult.isEmpty()){
            map.put("msg",0);
        }else{
            map.put("msg",1);
            map.put("tagList",tagListResult);
        }

        return map;
    }

    @PostMapping("/tag")
    public Map<String,Object> insertTag(Tag tag){
        Map<String,Object> map =  new HashMap<String,Object>();
        int result = tagService.insertTag(tag);
        if(result==1){
            map.put("msg",1);
            map.put("tag",tag);
        }
        return map;
    }

    @GetMapping("/tag")
    public Map<String,Object> selectAllTagByTagTypeId(@RequestParam(name="tagTypeId") String tagTypeId,
                                                      @RequestParam(name="page",defaultValue = "1") int page,
                                                      @RequestParam(name="size",defaultValue = "20") int size){
        Map<String,Object> map =  new HashMap<String,Object>();
        List<Tag> tagList = tagService.selectAllTagByTagTypeId(tagTypeId,page,size);
        PageResult pageResult = new PageResult();

        if(tagList.isEmpty()){
            map.put("msg",0);
        }else{
            map.put("msg",1);
            map.put("tagList",tagList);
            int count = tagService.selectTagCountByTagTypeId(tagTypeId);
            pageResult.setCount(count);
            pageResult.setCurrentPage(page);
            pageResult.setPageSize(size);
            pageResult.setPageCount(count,size);
            map.put("pageResult",pageResult);
        }
        return map;
    }

    @DeleteMapping("/tag/{tagId}")
    public Map<String,Object> deleteTagById(@PathVariable("tagId") String tagId){
        Map<String,Object> map = new HashMap<String,Object>();
        int tagResult1 = tagService.deleteTagById(tagId);
        int tagResult2 = tagArticleMapService.deletetagArticleMapByTagId(tagId);


        map.put("msg",1);

        return map;
    }

    //只搜索地点标签  通过标签名
    @GetMapping("/selectAddressTagByName/{tagName}")
    public Map<String,Object> selectAddressTagByName(@PathVariable("tagName") String tagName){
        Map<String,Object> map = new HashMap<String,Object>();
        List<Tag> tagList = tagService.selectAddressTagByName(tagName);
        if(tagList.size()==0){
            map.put("msg",0);
        }else{
            map.put("msg",1);
            map.put("tagList",tagList);
        }
        return map;
    }

    //查询所有标签
    @GetMapping("/selectAllTag")
    public Map<String,Object> selectAllTag(){
        Map<String,Object> map = new HashMap<String,Object>();
        List<Tag> tagList = tagService.selectAllTag();
        if(tagList.size()==0){
            map.put("msg",0);
        }else{
            map.put("msg",1);
            map.put("tagList",tagList);
        }
        return map;
    }

}
